<?php
/*******************************************************************************
 *
 *  Controller Name: 
 *
 *  Date: 
 *  
 *  Description: 
 *  
 *  Author: 
 *  
 *  Todo:
 *     NONE
 *
 *  Modification History:  
 * 
 *   Date         Name
 *  ----------   --------------
 *  2012-04-25   HAYDEN WAISANEN
 *  2012-04-23   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-16   HAYDEN WAISANEN
 *  2012-04-10   HAYDEN WAISANEN
 * 
 *****************************************************************************/
?>
<?php

class User extends CI_Controller {

   public function __construct()
   {
      parent::__construct();


   }

   public function index()
   {
   }
   public function logout()
   {
      $this->load->library('session');
      $this->session->sess_destroy();
      redirect('user/login');
   }

   public function whosonline()
   {
       $this->load->database();
       $query = $this->db->query('SELECT user_data FROM ci_sessions');
       foreach ($query->result_array() as $row)
       {
          $user = unserialize($row["user_data"]);
          $users[$user["UserID"]] = $user;
       }
       $data['users'] = $users;

        $this->load->view('whosonlineview',$data);

   }

   public function register()
   {
       //Load header with menu
      $this->load->helper('dashboard_helper');
      $this->load->view('headerview');


      $this->load->helper(array('form','url'));
      $this->load->library('form_validation');
      $this->load->library('encrypt');
      $this->load->model('User_model','user');

      //Manually access post arguments
      $username = $this->input->post('username', TRUE);
      $password = $this->input->post('password', TRUE);
      $params = "{$username},{$password}";


      $this->form_validation->set_rules('username', 'Username', 'is_unique[User.UserID]');
      $this->form_validation->set_rules('password', 'Password', 'required|matches[passwordconf]');
      $this->form_validation->set_rules('passwordconf', 'Password Confirmation', 'required');
      $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
      if($this->form_validation->run() == FALSE)
      {
         $this->load->view('registerformview');
      }
      else
      {
         $user = array(
         'UserID' => $this->input->post('username', TRUE),
         'Password' => 
         $this->encrypt->sha1($this->input->post('password', TRUE)),
         'Email' => $this->input->post('email', TRUE),
         'IsManager' => 0
         );
         $this->user->insert($user);
         redirect('user/login');
         
      }
       //Load the footer
      $this->load->view('footerview');


      
   }

   public function login()
   {
       //Load header with menu
      $this->load->helper('dashboard_helper');
      $this->load->view('headerview');

      //Check to see if there has been a submisson
      $this->load->helper(array('form','url'));
      $this->load->library('form_validation');
      $this->load->model('User_model','user');
      $this->load->library('session');

      //Manually access post arguments
      $username = $this->input->post('username', TRUE);
      $password = $this->input->post('password', TRUE);
      $params = "{$username},{$password}";


      $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
      $this->form_validation->set_rules('password', 'Password', "required|callback_check_password[{$params}]");
      if($this->form_validation->run() == FALSE)
      {
         $this->load->view('loginformview');
      }
      else
      {
         //Success!!
         //Set user credentials
         $result = $this->user->getUser($username);
         //$this->session->set_userdata($result[0]);
         $this->session->set_userdata('UserID',$result[0]['UserID']);
         $this->session->set_userdata('IsManager',$result[0]['IsManager']);
         $this->session->set_userdata('Email',$result[0]['Email']);


         redirect('/dashboard/index','location');
      }
       //Load the footer
      $this->load->view('footerview');


   }

   public function username_check($str)
   {
      $this->load->model('User_model','user');
      
      $result = $this->user->getUser($str);
      if(count($result) != 1)
      {
         $this->form_validation->set_message('username_check', $str.' does not exist');
         return FALSE;
      }
      else
      {
         return TRUE;
      }
   }

   public function check_password($str = '', $params = '')
   {
      list($username, $password) = explode(',', $params);
      $this->load->library('encrypt');

      $user = $this->user->getUser($username);
      $hash = $this->encrypt->sha1($password); 
      if(count($user) == 0)
      {
         return TRUE;
      }
      if( $hash == trim((string)$user[0]['Password'] ))
      {
         return TRUE;
      }
      else
      {
         $this->form_validation->set_message('check_password', 'Incorrect password');
         return FALSE;
      }
   }

   public function changePassword()
   {
       //Load header with menu
      $this->load->helper('dashboard_helper');
      $this->load->view('headerview');


      $this->load->helper(array('form','url'));
      $this->load->library('form_validation');
      $this->load->library('session');
      $this->load->library('encrypt');
      $this->load->model('User_model','user');

      //Manually access post arguments
      $username = $this->input->post('username', TRUE);
      $password = $this->input->post('password', TRUE);
      $params = "{$username},{$password}";


      $this->form_validation->set_rules('password', 'Password', 'required|matches[passwordconf]');
      $this->form_validation->set_rules('passwordconf', 'Password Confirmation', 'required');
      if($this->form_validation->run() == FALSE)
      {
         $this->load->view('changepasswordview');
      }
      else
      {
         $current_user = $this->session->all_userdata();
         $current_user["Password"] = $this->encrypt->sha1($this->input->post('password', TRUE));
         $this->user->update($current_user);
         echo "Success";
         $this->load->view('changepasswordview');
         
      }
       //Load the footer
      $this->load->view('footerview');


      
   }
      
}
